package com.example.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.request.ProductOrderPageRequest;
import com.example.manager.ProductOrderManager;
import com.example.mapper.ProductOrderMapper;
import com.example.model.ProductDO;
import com.example.model.ProductOrderDO;
import com.example.vo.ProductOrderVO;
import com.example.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/6/1316:41
 */
@Component
@Slf4j
public class ProductOrderManagerImpl implements ProductOrderManager {

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Override
    public int add(ProductOrderDO productOrderDO) {
        return productOrderMapper.insert(productOrderDO);
    }

    @Override
    public ProductOrderDO findByOutTradeNoAndAccountNo(String outTradeNo, Long accountNo) {
        return productOrderMapper.selectOne(new QueryWrapper<ProductOrderDO>()
                .eq("account_no", accountNo)
                .eq("out_trade_no", outTradeNo)
                .eq("del",0));
    }

    @Override
    public int updateOrderPayState(String outTradeNo, Long accountNo, String newState, String oldState) {
        return productOrderMapper.update(null,new UpdateWrapper<ProductOrderDO>()
                .eq("account_no", accountNo)
                .eq("out_trade_no", outTradeNo)
                .eq("state", oldState)
                .set("state", newState));
    }

    @Override
    public Map<String, Object> page(ProductOrderPageRequest request, Long accountNo) {
        Page<ProductOrderDO> pageInfo = new Page<>(request.getPage(),request.getSize());
        IPage<ProductOrderDO> orderDOIPage;
        if(StringUtils.isNotBlank(request.getState())){
            orderDOIPage = productOrderMapper.selectPage(pageInfo, new QueryWrapper<ProductOrderDO>()
                    .eq("account_no", accountNo)
                    .eq("del",0)
                    .eq("state", request.getState()));
        }else {
            orderDOIPage = productOrderMapper.selectPage(pageInfo, new QueryWrapper<ProductOrderDO>().eq("account_no", accountNo));
        }

        List<ProductOrderDO> records = orderDOIPage.getRecords();

        List<ProductOrderVO> productOrderVOList = records.stream().map(o -> beanProcess(o)).collect(Collectors.toList());

        HashMap<String, Object> result = new HashMap<>();

        result.put("total_record",orderDOIPage.getTotal());
        result.put("total_page",orderDOIPage.getPages());
        result.put("current_data",productOrderVOList);

        return result;
    }

    private ProductOrderVO beanProcess(ProductOrderDO productOrderDO){
        ProductOrderVO productOrderVO = new ProductOrderVO();
        BeanUtils.copyProperties(productOrderDO,productOrderVO);
        return productOrderVO;
    }
}
